package cn.jeefast.modules.job.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import cn.jeefast.common.utils.Constant.ScheduleStatus;
import cn.jeefast.modules.job.dao.ScheduleJobDao;
import cn.jeefast.modules.job.entity.ScheduleJob;
import cn.jeefast.modules.job.service.ScheduleJobService;
import cn.jeefast.modules.job.utils.ScheduleUtils;
import org.springframework.util.ObjectUtils;

@Service("scheduleJobService")
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, ScheduleJob> implements ScheduleJobService {
//	@Autowired(required=false)
//    private Scheduler scheduler;
	@Autowired
	private ScheduleJobDao schedulerJobDao;
	
	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init(){
		List<ScheduleJob> scheduleJobList = schedulerJobDao.selectList(null);
		for(ScheduleJob scheduleJob : scheduleJobList){
//			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
//            if(cronTrigger == null) {
//                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
//            }else {
//                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
//            }
		}
	}
	
	@Override
	@Transactional
	public boolean save(ScheduleJob scheduleJob) {
		scheduleJob.setCreateTime(new Date());
		scheduleJob.setStatus(ScheduleStatus.NORMAL.getValue());
        schedulerJobDao.insert(scheduleJob);
        
//        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
        return true;
    }
	
	@Override
	@Transactional
	public void update(ScheduleJob scheduleJob) {
//        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
                
        schedulerJobDao.updateById(scheduleJob);
    }

	@Override
	@Transactional
    public void deleteBatch(Long[] jobIds) {
//    	for(Long jobId : jobIds){
//    		ScheduleUtils.deleteScheduleJob(scheduler, jobId);
//    	}
    	
    	//删除数据
    	schedulerJobDao.deleteBatch(jobIds);
	}

	@Override
    public int updateBatch(Long[] jobIds, int status){
    	Map<String, Object> map = new HashMap<>();
    	map.put("list", jobIds);
    	map.put("status", status);
    	return schedulerJobDao.updateBatch(map);
    }
    
	@Override
	@Transactional
    public void run(Long[] jobIds) {
//    	for(Long jobId : jobIds){
//    		ScheduleUtils.run(scheduler, schedulerJobDao.selectById(jobId));
//    	}
    }

	@Override
	@Transactional
    public void pause(Long[] jobIds) {
//        for(Long jobId : jobIds){
//    		ScheduleUtils.pauseJob(scheduler, jobId);
//    	}
//
    	updateBatch(jobIds, ScheduleStatus.PAUSE.getValue());
    }

	@Override
	@Transactional
    public void resume(Long[] jobIds) {
//    	for(Long jobId : jobIds){
//    		ScheduleUtils.resumeJob(scheduler, jobId);
//    	}

    	updateBatch(jobIds, ScheduleStatus.NORMAL.getValue());
    }

	//@Override
	public IPage<ScheduleJob> queryPageList(Page<ScheduleJob> page, Map<String, Object> map) {
		QueryWrapper<ScheduleJob> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(!ObjectUtils.isEmpty(map.get("beanName")),"bean_name",map.get("beanName"));
		return schedulerJobDao.selectPage(page,queryWrapper);
	}

}
